﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TPLSample
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Task> tasks = new List<Task>();
            CancellationTokenSource cancelToken = new CancellationTokenSource(); 

            string result = String.Empty;

            Task t_MethodA = Task.Factory.StartNew(() =>
                {
                    result = Services.GetResultsMethodA();
                    cancelToken.Token.ThrowIfCancellationRequested(); 
                },cancelToken.Token);
            Task t_MethodB = Task.Factory.StartNew(() =>
                {
                    result = Services.GetResultsMethodB();
                    cancelToken.Token.ThrowIfCancellationRequested(); 
                },cancelToken.Token);

            tasks.Add(t_MethodA);
            tasks.Add(t_MethodB);

            Task.WaitAny(tasks.ToArray());
            cancelToken.Cancel(); 

            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}
